home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / jpeglib5b / jdapi.c < prev    next >
C/C++ Source or Header  |  1980-01-12  |  14KB  |  439 lines

  1. /*
  2.  * jdapi.c
  3.  *
  4.  * Copyright (C) 1994-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains application interface code for the decompression half of
  9.  * the JPEG library.  Most of the routines intended to be called directly by
  10.  * an application are in this file.  But also see jcomapi.c for routines
  11.  * shared by compression and decompression.
  12.  */
  13.  
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17.  
  18.  
  19. /*
  20.  * Initialization of a JPEG decompression object.
  21.  * The error manager must already be set up (in case memory manager fails).
  22.  */
  23.  
  24. GLOBAL void
  25. jpeg_create_decompress (j_decompress_ptr cinfo)
  26. {
  27.   int i;
  28.  
  29.   /* For debugging purposes, zero the whole master structure.
  30.    * But error manager pointer is already there, so save and restore it.
  31.    */
  32.   {
  33.     struct jpeg_error_mgr * err = cinfo->err;
  34.     MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
  35.     cinfo->err = err;
  36.   }
  37.   cinfo->is_decompressor = TRUE;
  38.  
  39.   /* Initialize a memory manager instance for this object */
  40.   jinit_memory_mgr((j_common_ptr) cinfo);
  41.  
  42.   /* Zero out pointers to permanent structures. */
  43.   cinfo->progress = NULL;
  44.   cinfo->src = NULL;
  45.  
  46.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  47.     cinfo->quant_tbl_ptrs[i] = NULL;
  48.  
  49.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  50.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  51.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  52.   }
  53.  
  54.   cinfo->sample_range_limit = NULL;
  55.  
  56.   /* Initialize marker processor so application can override methods
  57.    * for COM, APPn markers before calling jpeg_read_header.
  58.    */
  59.   cinfo->marker = NULL;
  60.   jinit_marker_reader(cinfo);
  61.  
  62.   /* OK, I'm ready */
  63.   cinfo->global_state = DSTATE_START;
  64. }
  65.  
  66.  
  67. /*
  68.  * Destruction of a JPEG decompression object
  69.  */
  70.  
  71. GLOBAL void
  72. jpeg_destroy_decompress (j_decompress_ptr cinfo)
  73. {
  74.   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  75. }
  76.  
  77.  
  78. /*
  79.  * Install a special processing method for COM or APPn markers.
  80.  */
  81.  
  82. GLOBAL void
  83. jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
  84.                jpeg_marker_parser_method routine)
  85. {
  86.   if (marker_code == JPEG_COM)
  87.     cinfo->marker->process_COM = routine;
  88.   else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
  89.     cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
  90.   else
  91.     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  92. }
  93.  
  94.  
  95. /*
  96.  * Set default decompression parameters.
  97.  */
  98.  
  99. LOCAL void
  100. default_decompress_parms (j_decompress_ptr cinfo)
  101. {
  102.   /* Guess the input colorspace, and set output colorspace accordingly. */
  103.   /* (Wish JPEG committee had provided a real way to specify this...) */
  104.   /* Note application may override our guesses. */
  105.   switch (cinfo->num_components) {
  106.   case 1:
  107.     cinfo->jpeg_color_space = JCS_GRAYSCALE;
  108.     cinfo->out_color_space = JCS_GRAYSCALE;
  109.     break;
  110.     
  111.   case 3:
  112.     if (cinfo->saw_JFIF_marker) {
  113.       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  114.     } else if (cinfo->saw_Adobe_marker) {
  115.       switch (cinfo->Adobe_transform) {
  116.       case 0:
  117.     cinfo->jpeg_color_space = JCS_RGB;
  118.     break;
  119.       case 1:
  120.     cinfo->jpeg_color_space = JCS_YCbCr;
  121.     break;
  122.       default:
  123.     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  124.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  125.     break;
  126.       }
  127.     } else {
  128.       /* Saw no special markers, try to guess from the component IDs */
  129.       int cid0 = cinfo->comp_info[0].component_id;
  130.       int cid1 = cinfo->comp_info[1].component_id;
  131.       int cid2 = cinfo->comp_info[2].component_id;
  132.  
  133.       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  134.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  135.       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  136.     cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  137.       else {
  138.     TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  139.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  140.       }
  141.     }
  142.     /* Always guess RGB is proper output colorspace. */
  143.     cinfo->out_color_space = JCS_RGB;
  144.     break;
  145.     
  146.   case 4:
  147.     if (cinfo->saw_Adobe_marker) {
  148.       switch (cinfo->Adobe_transform) {
  149.       case 0:
  150.     cinfo->jpeg_color_space = JCS_CMYK;
  151.     break;
  152.       case 2:
  153.     cinfo->jpeg_color_space = JCS_YCCK;
  154.     break;
  155.       default:
  156.     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  157.     cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  158.     break;
  159.       }
  160.     } else {
  161.       /* No special markers, assume straight CMYK. */
  162.       cinfo->jpeg_color_space = JCS_CMYK;
  163.     }
  164.     cinfo->out_color_space = JCS_CMYK;
  165.     break;
  166.     
  167.   default:
  168.     cinfo->jpeg_color_space = JCS_UNKNOWN;
  169.     cinfo->out_color_space = JCS_UNKNOWN;
  170.     break;
  171.   }
  172.  
  173.   /* Set defaults for other decompression parameters. */
  174.   cinfo->scale_num = 1;        /* 1:1 scaling */
  175.   cinfo->scale_denom = 1;
  176.   cinfo->output_gamma = 1.0;
  177.   cinfo->raw_data_out = FALSE;
  178.   cinfo->quantize_colors = FALSE;
  179.   /* We set these in case application only sets quantize_colors. */
  180.   cinfo->two_pass_quantize = TRUE;
  181.   cinfo->dither_mode = JDITHER_FS;
  182.   cinfo->desired_number_of_colors = 256;
  183.   cinfo->colormap = NULL;
  184.   /* DCT algorithm preference */
  185.   cinfo->dct_method = JDCT_DEFAULT;
  186.   cinfo->do_fancy_upsampling = TRUE;
  187. }
  188.  
  189.  
  190. /*
  191.  * Decompression startup: read start of JPEG datastream to see what's there.
  192.  * Need only initialize JPEG object and supply a data source before calling.
  193.  *
  194.  * This routine will read as far as the first SOS marker (ie, actual start of
  195.  * compressed data), and will save all tables and parameters in the JPEG
  196.  * object.  It will also initialize the decompression parameters to default
  197.  * values, and finally return JPEG_HEADER_OK.  On return, the application may
  198.  * adjust the decompression parameters and then call jpeg_start_decompress.
  199.  * (Or, if the application only wanted to determine the image parameters,
  200.  * the data need not be decompressed.  In that case, call jpeg_abort or
  201.  * jpeg_destroy to release any temporary space.)
  202.  * If an abbreviated (tables only) datastream is presented, the routine will
  203.  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
  204.  * re-use the JPEG object to read the abbreviated image datastream(s).
  205.  * It is unnecessary (but OK) to call jpeg_abort in this case.
  206.  * The JPEG_SUSPENDED return code only occurs if the data source module
  207.  * requests suspension of the decompressor.  In this case the application
  208.  * should load more source data and then re-call jpeg_read_header to resume
  209.  * processing.
  210.  * If a non-suspending data source is used and require_image is TRUE, then the
  211.  * return code need not be inspected since only JPEG_HEADER_OK is possible.
  212.  */
  213.  
  214. GLOBAL int
  215. jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
  216. {
  217.   int retcode;
  218.  
  219.   if (cinfo->global_state == DSTATE_START) {
  220.     /* First-time actions: reset appropriate modules */
  221.     (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  222.     (*cinfo->marker->reset_marker_reader) (cinfo);
  223.     (*cinfo->src->init_source) (cinfo);
  224.     cinfo->global_state = DSTATE_INHEADER;
  225.   } else if (cinfo->global_state != DSTATE_INHEADER) {
  226.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  227.   }
  228.  
  229.   retcode = (*cinfo->marker->read_markers) (cinfo);
  230.  
  231.   switch (retcode) {
  232.   case JPEG_HEADER_OK:        /* Found SOS, prepare to decompress */
  233.     /* Set up default parameters based on header data */
  234.     default_decompress_parms(cinfo);
  235.     /* Set global state: ready for start_decompress */
  236.     cinfo->global_state = DSTATE_READY;
  237.     break;
  238.  
  239.   case JPEG_HEADER_TABLES_ONLY:    /* Found EOI before any SOS */
  240.     if (cinfo->marker->saw_SOF)
  241.       ERREXIT(cinfo, JERR_SOF_NO_SOS);
  242.     if (require_image)        /* Complain if application wants an image */
  243.       ERREXIT(cinfo, JERR_NO_IMAGE);
  244.     /* We need not do any cleanup since only permanent storage (for DQT, DHT)
  245.      * has been allocated.
  246.      */
  247.     /* Set global state: ready for a new datastream */
  248.     cinfo->global_state = DSTATE_START;
  249.     break;
  250.  
  251.   case JPEG_SUSPENDED:        /* Had to suspend before end of headers */
  252.     /* no work */
  253.     break;
  254.   }
  255.  
  256.   return retcode;
  257. }
  258.  
  259.  
  260. /*
  261.  * Decompression initialization.
  262.  * jpeg_read_header must be completed before calling this.
  263.  *
  264.  * If a multipass operating mode was selected, this will do all but the
  265.  * last pass, and thus may take a great deal of time.
  266.  */
  267.  
  268. GLOBAL void
  269. jpeg_start_decompress (j_decompress_ptr cinfo)
  270. {
  271.   JDIMENSION chunk_ctr, last_chunk_ctr;
  272.  
  273.   if (cinfo->global_state != DSTATE_READY)
  274.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  275.   /* Perform master selection of active modules */
  276.   jinit_master_decompress(cinfo);
  277.   /* Do all but the final (output) pass, and set up for that one. */
  278.   for (;;) {
  279.     (*cinfo->master->prepare_for_pass) (cinfo);
  280.     if (cinfo->master->is_last_pass)
  281.       break;
  282.     chunk_ctr = 0;
  283.     while (chunk_ctr < cinfo->main->num_chunks) {
  284.       /* Call progress monitor hook if present */
  285.       if (cinfo->progress != NULL) {
  286.     cinfo->progress->pass_counter = (long) chunk_ctr;
  287.     cinfo->progress->pass_limit = (long) cinfo->main->num_chunks;
  288.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  289.       }
  290.       /* Process some data */
  291.       last_chunk_ctr = chunk_ctr;
  292.       (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
  293.                     &chunk_ctr, (JDIMENSION) 0);
  294.       if (chunk_ctr == last_chunk_ctr) /* check for failure to make progress */
  295.     ERREXIT(cinfo, JERR_CANT_SUSPEND);
  296.     }
  297.     (*cinfo->master->finish_pass) (cinfo);
  298.   }
  299.   /* Ready for application to drive last pass through jpeg_read_scanlines
  300.    * or jpeg_read_raw_data.
  301.    */
  302.   cinfo->output_scanline = 0;
  303.   cinfo->global_state = (cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING);
  304. }
  305.  
  306.  
  307. /*
  308.  * Read some scanlines of data from the JPEG decompressor.
  309.  *
  310.  * The return value will be the number of lines actually read.
  311.  * This may be less than the number requested in several cases,
  312.  * including bottom of image, data source suspension, and operating
  313.  * modes that emit multiple scanlines at a time.
  314.  *
  315.  * Note: we warn about excess calls to jpeg_read_scanlines() since
  316.  * this likely signals an application programmer error.  However,
  317.  * an oversize buffer (max_lines > scanlines remaining) is not an error.
  318.  */
  319.  
  320. GLOBAL JDIMENSION
  321. jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
  322.              JDIMENSION max_lines)
  323. {
  324.   JDIMENSION row_ctr;
  325.  
  326.   if (cinfo->global_state != DSTATE_SCANNING)
  327.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  328.   if (cinfo->output_scanline >= cinfo->output_height)
  329.     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  330.  
  331.   /* Call progress monitor hook if present */
  332.   if (cinfo->progress != NULL) {
  333.     cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  334.     cinfo->progress->pass_limit = (long) cinfo->output_height;
  335.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  336.   }
  337.  
  338.   /* Process some data */
  339.   row_ctr = 0;
  340.   (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
  341.   cinfo->output_scanline += row_ctr;
  342.   return row_ctr;
  343. }
  344.  
  345.  
  346. /*
  347.  * Alternate entry point to read raw data.
  348.  * Processes exactly one iMCU row per call, unless suspended.
  349.  */
  350.  
  351. GLOBAL JDIMENSION
  352. jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
  353.             JDIMENSION max_lines)
  354. {
  355.   JDIMENSION lines_per_iMCU_row;
  356.  
  357.   if (cinfo->global_state != DSTATE_RAW_OK)
  358.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  359.   if (cinfo->output_scanline >= cinfo->output_height) {
  360.     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  361.     return 0;
  362.   }
  363.  
  364.   /* Call progress monitor hook if present */
  365.   if (cinfo->progress != NULL) {
  366.     cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  367.     cinfo->progress->pass_limit = (long) cinfo->output_height;
  368.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  369.   }
  370.  
  371.   /* Verify that at least one iMCU row can be returned. */
  372.   lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
  373.   if (max_lines < lines_per_iMCU_row)
  374.     ERREXIT(cinfo, JERR_BUFFER_SIZE);
  375.  
  376.   /* Decompress directly into user's buffer. */
  377.   if (! (*cinfo->coef->decompress_data) (cinfo, data))
  378.     return 0;            /* suspension forced, can do nothing more */
  379.  
  380.   /* OK, we processed one iMCU row. */
  381.   cinfo->output_scanline += lines_per_iMCU_row;
  382.   return lines_per_iMCU_row;
  383. }
  384.  
  385.  
  386. /*
  387.  * Finish JPEG decompression.
  388.  *
  389.  * This will normally just verify the file trailer and release temp storage.
  390.  *
  391.  * Returns FALSE if suspended.  The return value need be inspected only if
  392.  * a suspending data source is used.
  393.  */
  394.  
  395. GLOBAL boolean
  396. jpeg_finish_decompress (j_decompress_ptr cinfo)
  397. {
  398.   if (cinfo->global_state == DSTATE_SCANNING ||
  399.       cinfo->global_state == DSTATE_RAW_OK) {
  400.     /* Terminate final pass */
  401.     if (cinfo->output_scanline < cinfo->output_height)
  402.       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  403.     (*cinfo->master->finish_pass) (cinfo);
  404.     cinfo->global_state = DSTATE_STOPPING;
  405.   } else if (cinfo->global_state != DSTATE_STOPPING) {
  406.     /* Repeat call after a suspension? */
  407.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  408.   }
  409.   /* Check for EOI in source file, unless master control already read it */
  410.   if (! cinfo->master->eoi_processed) {
  411.     switch ((*cinfo->marker->read_markers) (cinfo)) {
  412.     case JPEG_HEADER_OK:    /* Found SOS!? */
  413.       ERREXIT(cinfo, JERR_EOI_EXPECTED);
  414.       break;
  415.     case JPEG_HEADER_TABLES_ONLY: /* Found EOI, A-OK */
  416.       break;
  417.     case JPEG_SUSPENDED:    /* Suspend, come back later */
  418.       return FALSE;
  419.     }
  420.   }
  421.   /* Do final cleanup */
  422.   (*cinfo->src->term_source) (cinfo);
  423.   /* We can use jpeg_abort to release memory and reset global_state */
  424.   jpeg_abort((j_common_ptr) cinfo);
  425.   return TRUE;
  426. }
  427.  
  428.  
  429. /*
  430.  * Abort processing of a JPEG decompression operation,
  431.  * but don't destroy the object itself.
  432.  */
  433.  
  434. GLOBAL void
  435. jpeg_abort_decompress (j_decompress_ptr cinfo)
  436. {
  437.   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  438. }
  439.